﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class stats : MonoBehaviour
{
    private Image img;

    [SerializeField]
    private Text statTxt;

    [SerializeField]
    private float lerpSpd;
    [SerializeField]
    public bool showSlash;

    public bool IsFull
    {
        get { return img.fillAmount == 1; }
        
    }

    private float curVal;
    private float fill;
    private float overflow;
    public float maxVal { get; set; }
    public float myVal
    {
        get
        {
            return curVal;
        }
        set
        {
           
            if (value > maxVal)
            {

                overflow = value - maxVal;
                curVal = maxVal;
            }
            else if(value<0){
                curVal = 0;
            }
            else
            {
                curVal = value;
            }
            fill = curVal / maxVal;
            if(statTxt != null)
            {
                if (showSlash == true)
                {
                    statTxt.text = curVal + "/" + maxVal;
                }
                else
                {
                    statTxt.text = curVal.ToString();
                }
            }
            
        }
    }

    public Text MyStatTxt { get => statTxt; set => statTxt = value; }


    // Start is called before the first frame update
    void Start()
    {
        img = GetComponent<Image>();
      
    }

    // Update is called once per frame
    void Update()
    {
        if(fill != img.fillAmount)
        {
            img.fillAmount = Mathf.MoveTowards(img.fillAmount, fill, lerpSpd*Time.deltaTime);
        }
        
        
    }
    public void inicialize(float cur, float max)
    {
        maxVal = max;
        myVal = cur;
    }
    public void ResetValue()
    {
        
        img.fillAmount = 0;
    }
    public float MyOverflow
    {
        get
        {
            float overtmp=overflow;
            
            overflow = 0;
            return overtmp;
        }
    }
    public void Improve(int initStat)
    {
        inicialize((initStat * Player.MyInstance.MyLevel), (initStat * Player.MyInstance.MyLevel));
        
    }
}
